﻿<!---------------------------------------------------------------
// 文件名称: payment.js
// 描   述: 脚本语言
// 修改标识: Modify By 李传 2008-11-17
// 修改内容: 不强制选择配送 
//          删除判断
//------------------------------------------------------------->
<!---------------------------------------------------------------
// 文件名称: payment.js
// 描   述: 脚本语言
// 修改标识: Modify By 李传 2008-11-19
// 修改内容: 无配送选择 没信息 
//          添加默认信息
//------------------------------------------------------------->
// JavaScript Document
var templevel;
//修改购物车数量并直接进入下一步
function gotopay(){
	document.getElementById("direct").value = "go";
	$("#form1")[0].submit();
}
//判断配送方式
function changecat(obj, level){
	var id = obj.options[obj.options.selectedIndex].value;
	 if(id == "")return;
	 //李传 2008-11-19
	//$('#weightmoney')[0].innerHTML = "该地区未选择配送，运费为0！！";	
	var url = baseurl + "cartstep1.aspx?act=delivery&id=" + id + "&d=" + escape(Date());
	$('#weightlist')[0].innerHTML = "loading...";

	//Ajax请求
	$("#weightlist").load(url,recorddeliverycache); 

	$('#areafid_' + level + "_cache")[0].value = $('#areafid_' + level)[0].innerHTML;	
	
	level = level + 1;
	url = baseurl + "cartstep1.aspx?act=area&id=" + id + "&level=" + level + "&d=" + escape(Date());

	$('#areafid_' + level)[0].style.display = "";
	$('#areafid_' + level)[0].innerHTML = "loading...";
	templevel = level;
	
	//Ajax请求
	$('#areafid_' + level).load(url,recordcache); 
}

//判断配送方式-团购用
function changecatgroupbuy(obj, level){
	var id = obj.options[obj.options.selectedIndex].value;
	if(id == "") return;
	//$('#weightmoney')[0].innerHTML = "该地区无配送选择，运费为0！！";
	var url = baseurl + "/groupbuystep.aspx?act=delivery&id=" + id + "&d=" + escape(Date());
	$('#weightlist')[0].innerHTML = "loading...";

	//ajax请求
	$("#weightlist").load(url);   
	
	$('#areafid_' + level + "_cache")[0].value = $('#areafid_' + level)[0].innerHTML;	
	
	level = level + 1;
	url = baseurl + "/groupbuystep.aspx?act=area&id=" + id + "&level=" + level + "&d=" + escape(Date());

	$('#areafid_' + level)[0].style.display = "";
	$('#areafid_' + level)[0].innerHTML = "loading...";
	templevel = level;

	//ajax请求
	$('#areafid_' + level).load(url);   
}

//记录选中地区缓存
function recordcache(){
	$('#areafid_' + templevel + '_cache')[0].value = $('#areafid_' + templevel)[0].innerHTML;	
}
//记录计算公式缓存
function recorddeliverycache(){
	$('#weightlist_cache')[0].value = $('#weightlist')[0].innerHTML;	
}
//加载STEP1页面缓存
function initcache(){
	$('#areafid_2')[0].innerHTML = $('#areafid_2_cache')[0].value;
	$('#areafid_3')[0].innerHTML = $('#areafid_3_cache')[0].value;
	$('#areafid_4')[0].innerHTML = $('#areafid_4_cache')[0].value;
	$('#weightlist')[0].innerHTML = $('#weightlist_cache')[0].value;
	$('#weightmoney')[0].innerHTML = $('#weightmoney_cache')[0].value;
}
//加载STEP2页面缓存
function initcache2(){
	var cache = $("#page_cache")[0].value.split(",");

	//$('#usedscore')[0].value = cache[0];
	//$('#usedmoney')[0].value = cache[1];
	if($('#couponlist')[0]){
		$('#couponlist')[0].style.display = cache[0];
	}
	if(cache[1] == "1"){
		$("#ok")[0].style.display = ""
	}else if(cache[1] == "2"){
		$("#error")[0].style.display = ""
	}
	$('#newtotalprice')[0].innerHTML = cache[2];
	
	if(cache[3] == "1") $("#verify")[0].disabled = false;
	//优惠卷价格
	$('#couponmoney')[0].innerHTML = cache[4];
}
//生成STEP2页面缓存
function CreateCache2(){
	var index = 0;
	var verify = 0;
	//提示
	if($("#ok")[0].style.display == ""){
		index = 1;
	}else if($("#error")[0].style.display == ""){
		index = 2;
	}
	//是否启用
	if($("#verify")[0].disabled == false)
		verify = 1;
			
	var cache = $('#couponlist')[0].style.display + "," + index + "," + $('#newtotalprice')[0].innerHTML + "," + verify + "," + $('#couponmoney')[0].innerHTML;
	$("#page_cache")[0].value = cache;
	//alert(cache);
}
//更换配送方式得出计算公式
function changedelivery(obj){
	
    if (navigator.appVersion.indexOf("MSIE") == -1){
		var query = obj.attributes['query'].nodeValue;
	}else{
		var query = obj.query;
	}
	
	var dll = obj.value;
	var url = baseurl + "cartstep1.aspx?act=payment&query=" + query + "&dll=" + dll + "&d=" + escape(Date());
	$('#weightmoney')[0].innerHTML = "loading...";
	recorddeliverycache();

	//Ajax请求
	$("#weightmoney").load(url,recordmoneycache); 
}
//记录运费金额并保存到INPUT里面
function recordmoneycache(){
	$('#weightmoney_cache')[0].value = $('#weightmoney')[0].innerHTML;	
}
//STEP1页面提交判断
function checkstep1form(){
	//吕鑫 2009-04-18 兼容FIREFOX代码修正

	if(!CheckSelected("delivery")){
		alert("请选择配送方式");
		document.getElementById('areafid_1').focus();
		return false;
	}
	
	//判断配送地区
	if(document.getElementById('areafid_1') && document.getElementById('areafid_1').options[0].selected)
	{
		alert("请选择配送地区");	
		return false;
	}
	//判断支付方式
	if(!CheckSelected("paymethod")){
		alert("请选择支付方式");	
		return false;
	}	
	
	//判断收货人
	if(document.getElementsByName("receiver") && document.getElementsByName("receiver")[0].value != ""){
	}else{
		alert("请填写收货人");	
		document.getElementsByName("receiver")[0].focus();
		return false;
	}	
	
	//判断收货人
	if(document.getElementsByName("addr") && document.getElementsByName("addr")[0].value != ""){
	}else{
		alert("请填写收货地址");
		document.getElementsByName("addr")[0].focus();	
		return false;
	}	
	//判断邮政编码
	/*if($("#post")[0].value == ""){
		alert("请填写收货地址");
		$("#post")[0].focus();	
		return false;
	}	*/
	//判断至少一个联系方式
	if(document.getElementsByName("tel")[0].value == "" && document.getElementsByName("mobile")[0].value == ""){
		alert("请至少填写一个联系方式");
		if(document.getElementsByName("tel")[0].value == ""){
			document.getElementsByName("tel")[0].focus();	
		}
		if(document.getElementsByName("mobile")[0].value == ""){
			document.getElementsByName("mobile")[0].focus();	
		}
		return false;
	}	
	
	//判断Email
	if(document.getElementsByName("email")[0].value == ""){
		alert("请填写EMAIL");
		document.getElementsByName("email")[0].focus();	
		return false;
	}	
	//EMAIL格式验证
	if(!ValidateEmail(document.getElementsByName("email")[0].value)){
		alert("EMAIL格式不正确");
		document.getElementsByName("email")[0].focus();	
		return false;
	}

	return true;
}
//判断RADIO是否选中
function CheckSelected(id)
{
   var eless = document.getElementsByName(id);
   for(var i=0;i<eless.length;i++){
     if(eless[i].checked){
     	return true;
     }
   }
   return false;
} 

function   ValidateEmail(j) 
{ 
	var   emailReg=/^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; 
	return   emailReg.test(j); 
}

//根据CHECKBOX显示或隐藏区域
function ShowHiddenInfo(cid, areaid, status){
	if($("#"+cid)[0].checked == status){
		$("#"+areaid)[0].style.display = "none";
	}else{
		$("#"+areaid)[0].style.display = "";
	}	
}
//加载地址缓存
function InitAddress(obj){
	var ary = obj.value.split("|");
	try{
		document.getElementsByName("receiver")[0].value = ary[0];
		document.getElementsByName("addr")[0].value = ary[1];
		document.getElementsByName("post")[0].value = ary[2];
		document.getElementsByName("tel")[0].value = ary[3];
		document.getElementsByName("mobile")[0].value = ary[4];
		document.getElementsByName("email")[0].value = ary[5];
		document.getElementsByName("building")[0].value = ary[6];
	}catch(e){}
}
//把选中的优惠卷加入
function InitCoupon(obj){
	var ary = obj.value.split("-");
	try{
		$("#coupon1")[0].value = ary[0];
		$("#coupon2")[0].value = ary[1];
		$("#coupon3")[0].value = ary[2];
		$("#coupon4")[0].value = ary[3];
		
		verifycoupon();
	}catch(e){}
}
//优惠卷输入直接到下一个INPUT
function movenext(obj, index){
	obj.value = obj.value.toUpperCase();
	if(obj.value.length == 4 && index != 4){
		$("#coupon" + (index+1))[0].select();
		$("#coupon" + (index+1))[0].focus();
	}
}
//验证优惠卷是否可用
function verifycoupon(){
	var coupon = $("#coupon1")[0].value + "-" + $("#coupon2")[0].value + "-" + $("#coupon3")[0].value + "-" + $("#coupon4")[0].value;
	var url = baseurl + "cartstep1.aspx?act=coupon&coupon=" + coupon + "&d=" + escape(Date());
	
	$("#load")[0].style.display = "";
	$("#ok")[0].style.display = "none";
	$("#error")[0].style.display = "none";
		
	//Ajax请求
	$("#ispasstxt").load(url,showcouponmsg); 
	
	$("#verify")[0].checked = false;
	$("#verify")[0].disabled = true;
	MathPrice();
	$("#couponprice")[0].value = "0";
}
//提示优惠卷判断结果
function showcouponmsg(){
	var ispass = $("#ispasstxt")[0].innerHTML;
	$("#load")[0].style.display = "none";

	if(isNaN(ispass)){
		if(ispass == "none"){
			$("#ok")[0].style.display = "none";
			$("#error")[0].style.display = "";
			$("#errmsg")[0].innerHTML = "× 验证错误，使用该券不存在或已被使用";
		}else{
			$("#ok")[0].style.display = "none";
			$("#error")[0].style.display = "";
			$("#errmsg")[0].innerHTML = "× 验证错误，" + ispass;
		}
	}else{
		$("#ok")[0].style.display = "";
		$("#error")[0].style.display = "none";
		$("#couponmoney")[0].innerHTML = ispass;
		$("#verify")[0].disabled = false;
	}
	CreateCache2();
}
//根据积分计算抵扣金额
function changemoney(){
	if($("#changebutton")[0].value == "取消积分抵扣"){
		$("#usedscore")[0].value = 0;
		$("input[name='usedmoney']")[0].value = 0;
		$("#changebutton")[0].value = "积分抵扣";
	}else{
		var score = $("#usedscore")[0].value;
		var money = score / scorepercent;
		
		$("input[name='usedmoney']")[0].value = money;
		if(money != 0){
			//$("#changebutton")[0].value = "取消积分抵扣";
		}
	}
	
	MathPrice();
}
//根据积分变动重新计算总价格
function MathPrice(){
	var newprice = Math.round(($("#totalprice")[0].value/10)*1000)/100 - Math.round(($("input[name='usedmoney']")[0].value/10)*1000)/100;
	newprice = Math.round((newprice/10)*1000)/100;
	$("#newtotalprice")[0].innerHTML = newprice;	
}
//判断输入的积分是否合法
function checkscore(obj){
	var score = $("#userscore")[0].value;
	var maxscore = $("#maxscore")[0].value;
	var cscore = obj.value;
	
	if(isNaN(cscore) || cscore.indexOf('.') != -1 || cscore.indexOf('-') != -1){
		alert("您的积分输入不合法");
		obj.focus();
		return false;
	}
	
	if(parseInt(score) < parseInt(cscore)){
		alert("您最多能使用"+score+"积分");
		obj.focus();
		return false;
	}
	
	if(parseInt(cscore) > parseInt(maxscore)){
		if(maxscore == "0"){
			alert("该订单不可使用积分");
			obj.focus();
			return false;
		}else{
			alert("该订单您最多可以使用"+maxscore+"积分");
			obj.focus();
			return false;
		}
	}
	
	changemoney();
	usecoupon();
	
	CreateCache2();
	return true;
}
//根据选择的优惠卷重新计算价格
function usecoupon(){
	if($("#verify")[0].checked == true){
		var newprice = Math.round(($("#totalprice")[0].value/10)*1000)/100 - Math.round(($("#couponmoney")[0].innerHTML/10)*1000)/100 - Math.round(($("input[name='usedmoney']")[0].value/10)*1000)/100;
		newprice = Math.round((newprice/10)*1000)/100;
		if(newprice < 0) newprice = 0;
		$("#newtotalprice")[0].innerHTML = newprice;
		$("#couponprice")[0].value = $("#couponmoney")[0].innerHTML;
	}else{
		MathPrice();	
		$("#couponprice")[0].value = "0";
	}
	
	CreateCache2();
}
//STEP2提交验证
function checkform2(){
	$("input[name='usedmoney']")[0].disabled = false;
	return true;	
}

//判断是否为货到付款
function CheckCod(thisobj){
	var obj = null;
	var eless = document.getElementsByName("delivery");
	for(var i=0;i<eless.length;i++){
	 if(eless[i].checked){
		obj = eless[i];
	 }
	}
	
	if(thisobj.value == "COD" && obj == null){
		alert("请先选择配送方式");
		return false;	
	}
	
	var iscod = null;
    if (navigator.appVersion.indexOf("MSIE") == -1){
		iscod = obj.getAttribute("iscod");
	}else{
		iscod = obj.iscod;
	}
	if(thisobj.value == "COD" && iscod == 0){
		alert("该配送方式不支持货到付款");
		return false;
	}else{
		return true;	
	}
}
